package com.iotechn.unimall.admin.api.equipment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.EquipmentTypeDO;
import com.iotechn.unimall.data.mapper.EquipmentTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;


/**
 * Generate Code By Unimall
 */
@Service
public class equipmentTypeServiceImpl implements equipmentTypeService {

    @Autowired
    private EquipmentTypeMapper equipmentTypeMapper;

    @Override
    public boolean delete(Long id,  Long adminId) throws ServiceException {
        return equipmentTypeMapper.deleteById(id) > 0;
    }

    @Override
    public List<EquipmentTypeDO> xiaList(Long adminId) throws ServiceException {
        List<EquipmentTypeDO> equipmentTypeDOS = equipmentTypeMapper.selectList(new QueryWrapper<EquipmentTypeDO>());
        return equipmentTypeDOS;
    }

    @Override
    public Page<EquipmentTypeDO> list(String name, Integer page, Integer limit, Long adminId) throws ServiceException {
        QueryWrapper<EquipmentTypeDO> wrapper = new QueryWrapper<EquipmentTypeDO>();
        if (name != null) {
            wrapper.like("name", name);
        }
        return equipmentTypeMapper.selectPage(Page.div(page, limit, EquipmentTypeDO.class), wrapper.orderByDesc("id"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EquipmentTypeDO create(String name, Long adminId) throws ServiceException {
         Date now = new Date();
         EquipmentTypeDO insertDO = new EquipmentTypeDO();
         insertDO.setName(name);
         insertDO.setGmtUpdate(now);
         insertDO.setGmtCreate(now);
         if (equipmentTypeMapper.insert(insertDO) > 0) {
             return insertDO;
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, String name, Long adminId) throws ServiceException {
         Date now = new Date();
         EquipmentTypeDO updateDO = new EquipmentTypeDO();
         updateDO.setId(id);
         updateDO.setName(name);
         updateDO.setGmtUpdate(now);
         if (equipmentTypeMapper.updateById(updateDO) > 0) {
             return "ok";
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

}
